package com.analy.core;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.common.cache.Cache;
import com.common.utils.CacheUtil;
import com.common.vo.RatioDataInput;
import com.common.vo.SnatchItem;
import com.common.vo.SnatchTask;
import com.common.vo.SnatchVersion;
import com.work.service.SnatchControl;
import com.work.service.SnatchService;
import com.work.vo.SnatchData;

public class AnalyThread extends Thread {

	protected Logger logger = Logger.getLogger(AnalyThread.class);
	
	private Cache cache;

	private SnatchTask snatchTask;
	
	private SnatchItem snatchItem;
	
	private boolean flag = true;// 抓取线程运行和销毁的信号量
	
	private long systemSleppTime=50L;
	
	private SnatchService hgSnatchDataImpl;
	
	private SnatchVersion snatchVersion;
	
	@Override
	public void run() {
		while (flag) {
			try {
				Thread.sleep(systemSleppTime);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			try {
				if (flag) {
					SnatchVersion cacheVersion = findVersion();
					if(null ==cacheVersion){
						continue;
					}
					if(null == snatchVersion){
						snatchVersion = cacheVersion;
					}else{
						if(snatchVersion.getVersion()>=cacheVersion.getVersion()){
							snatchVersion = cacheVersion;
							continue;
						}
					}
					
					String[] snatchdatas = findSnatchDatas(snatchTask.getTaskCode(),snatchVersion.getCount());
					if(null == snatchdatas){
						continue;
					}
					
					SnatchData snatchData = new SnatchData();
					snatchData.setSnatchTask(snatchTask);
					snatchData.setSnatchVersion(cacheVersion);
					snatchData.setDatas(snatchdatas);
					
					RatioDataInput ratioDataInput = hgSnatchDataImpl.gatherData(snatchData);
					System.out.println(ratioDataInput);
					
					
				}
				// logger.info(nodeTaskInput.getTaskCode()+" 版本号： "+ratioDataInput.getVersion()+" 采集总结束："+(System.currentTimeMillis()-time));
			} catch (Exception e) {
				logger.error("采集任务错误", e);
				snatchItem.setDescribe("采集报错！");
			} 
		}
		logger.info("采集任务结束" + snatchTask);

	}
	
	
	
	public void init(SnatchTask snatchTask,SnatchItem snatchItem,Cache cache){
		this.snatchTask = snatchTask;
		this.snatchItem = snatchItem;
		this.cache = cache;
		hgSnatchDataImpl = SnatchControl.getSnatchService(snatchTask.getWebCode());
	}
	
	public void destroy(){
		flag = false;
	}
	
	/**
	 * 获取当前版本号
	 * @return
	 */
	private SnatchVersion findVersion(){
		String vkey = CacheUtil.getVersionKey(snatchTask.getTaskCode());
		SnatchVersion csv = (SnatchVersion)cache.get(vkey);
		return csv;
	}
	
	private String[] findSnatchDatas(String key,int page){
		List<String> l = new ArrayList<String>();
		for(int i=0;i<page;i++){
			l.add(CacheUtil.getValueKey(key, i));
		}
		
		List<Object> list = cache.getMore(l);
		String[] a = new String[list.size()];
		for(int i = 0;i<list.size();i++){
			a[i] = (String) list.get(i);
		}
		return a;
	}
	
	

}
